//
//  ZTLinkageInfo.m
//  YSSmartHome
//
//  Created by Yahaong on 16/9/6.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "ZTLinkageInfo.h"

@implementation ZTLinkageInfo
- (id)init{
    
    if (self = [super init]) {
        
//        _datas = NULL;
    }
    
    return self;
}

- (void)dealloc{
    
//    if (_datas != NULL) {
//        
//        purgePacketParamData(_datas);
//        _datas = NULL;
//    }
}

//- (void) setApsn:(XAITYPEAPSN)apsn Luid:(XAITYPELUID)luid ID:(int)ID Datas:(packet_param_data *)datas{
//    _dev_apsn = apsn;
//    _dev_luid = luid;
//    _some_id = ID;
//    if (_datas != NULL) {
//        purgePacketParamData(_datas);
//    }
//    _datas =  generateParamDataCopyOther(datas);
//}
//- (void) setApsn:(XAITYPEAPSN)apsn Luid:(XAITYPELUID)luid ID:(int)ID device:(ZTDevice *)device Datas:(packet_param_data*)datas
//{
//    _dev_apsn = apsn;
//    _dev_luid = luid;
//    _some_id = ID;
//    _device =device;
//    if (_datas != NULL) {
//        purgePacketParamData(_datas);
//    }
//    _datas =  generateParamDataCopyOther(datas);
//}
@end

@implementation ZTLinkageInfoCtrl

@end

@implementation ZTLinkageInfoStatus

@end

@implementation ZTLinkageInfoStatusMutable

@end

@implementation LinkageUseInfoTime
-(void)change{
    
//    packet_param_data* time_data = generatePacketParamData();
//    
//    if (_timeStruct != nil) {
//        NSData* bData = nil;
//        param_data_set(time_data, XAI_DATA_TYPE_BIN_DATE, bData.length, (void*)bData.bytes, NULL);
//        
//    }else{
//        param_data_set(time_data, XAI_DATA_TYPE_BIN_DELAY, sizeof(MQTTTYPETime), &_time, NULL);
//    }
    
//    [self setApsn:0 Luid:0 ID:0 Datas:time_data];
//    [self setApsn:0 Luid:0 ID:0 Datas:(__bridge id)(time_data) needChange:false];
    
//    purgePacketParamData(time_data);
}


//
////needchange  是否需要更新内部数据
//- (void) setApsn:(XAITYPEAPSN)apsn Luid:(XAITYPELUID)luid ID:(int)ID Datas:(_xai_packet_param_data*)datas needChange:(BOOL)needChange{
//    
//    [super setApsn:apsn Luid:luid ID:ID Datas:datas];
//    
//    if (needChange == false) return;
//    
//    if (datas != nil && datas->data_type == XAI_DATA_TYPE_DELAY) {
//        
//        XAITYPETime time = 0;
//        byte_data_copy(&time, datas->data, sizeof(XAITYPETime), datas->data_len);
//        
//        _time = time;
//        
//    }else if(datas != nil && datas->data_type == XAI_DATA_TYPE_Time){
//        
//        XAILinkageTimeStruct* timeStruct = [[XAILinkageTimeStruct alloc] init];
//        
//        NSData* bData = [NSData dataWithBytes:datas->data length:datas->data_len];
//        [timeStruct setDateFromBData:bData];
//        
//        _timeStruct = timeStruct;
//    }
//    
//}


@end
@implementation LinkageTimeStruct

- (void)setDate:(NSDate*)date wdays:(NSArray *)wdays dimension:(Linkage_Time_Dimension)dimension{
    
    _dimension = dimension;
    _flag = 0;
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/BeiJing"];
    NSDateFormatter* hourFormat = [[NSDateFormatter alloc] init];
    [hourFormat setTimeZone: timeZone];
    [hourFormat setDateFormat:@"HH"];
    
    NSDateFormatter* minuFormat = [[NSDateFormatter alloc] init];
    minuFormat.timeZone = timeZone;
    [minuFormat setDateFormat:@"mm"];
    
    NSDateFormatter* secFormat = [[NSDateFormatter alloc] init];
    secFormat.timeZone = timeZone;
    [secFormat setDateFormat:@"ss"];
    
    NSDateFormatter* yearFormat = [[NSDateFormatter alloc] init];
    yearFormat.timeZone = timeZone;
    [yearFormat setDateFormat:@"yyyy"];
    
    NSDateFormatter* mouthFormat = [[NSDateFormatter alloc] init];
    mouthFormat.timeZone = timeZone;
    [mouthFormat setDateFormat:@"MM"];
    
    NSDateFormatter* dayFormat = [[NSDateFormatter alloc] init];
    dayFormat.timeZone = timeZone;
    [dayFormat setDateFormat:@"dd"];
    _hour =[[hourFormat stringFromDate:date] intValue];
    _min = [[minuFormat stringFromDate:date] intValue];
    _sec = [[secFormat stringFromDate:date] intValue];
    uint64_t year = [[yearFormat stringFromDate:date] intValue] - 2000; //从2千年开始计算
    uint64_t i = 1;
    _year |= (i << year);
    uint16_t mouth = [[mouthFormat stringFromDate:date] intValue];
    BIT_SET(_mouth, mouth);
    uint32_t  mday = [[dayFormat stringFromDate:date] intValue];
    BIT_SET(_mday, mday);
    if (dimension == Linkage_Time_Dimension_Day) {
        for (int i = 0; i < sizeof(_year) * 8; i++) {
            BIT_SET(_year, i);
        }
        for (int i = 0; i < sizeof(_mouth) * 8; i++) {
            BIT_SET(_mouth, i);
        }
        for (int i = 0; i < sizeof(_mday) * 8; i++) {
            BIT_SET(_mday, i);
        }
        _sec = 0;
    }else if (dimension == Linkage_Time_Dimension_Mouth){
        for (int i = 0; i < sizeof(_year) * 8; i++) {
            BIT_SET(_year, i);
        }
        for (int i = 0; i < sizeof(_mouth) * 8; i++) {
            BIT_SET(_mouth, i);
        }
        
    }else if (dimension == Linkage_Time_Dimension_Year){
        for (int i = 0; i < sizeof(_year) * 8; i++) {
            BIT_SET(_year, i);
        }
    }
    _wday = 0;
    for (NSNumber *num in wdays) {
        uint8_t index = [num intValue];
        BIT_SET(_wday, index);
    }
    if (_wday) {
        _mday = 0;
    }

}
-(uint8_t *)getBinary{
    uint8_t *buff = malloc(19);
    uint8_t index = 0;
    memcpy(buff, &_flag, 1);
    index += 1;
    memcpy(buff+index, &_wday,1);
    index += 1;
    uint64_t bigYear = CFSwapInt64(_year);
    memcpy(buff+index, &bigYear,  8);
    index += 8;
    uint16_t bigMouth = CFSwapInt16(_mouth);
    memcpy(buff+index, &bigMouth,  2);
    index += 2;
    uint32_t bigDay = CFSwapInt32(_mday);
    memcpy(buff+index, &bigDay,  4);
    index += 4;
    memcpy(buff+index, &_hour,1);
    index += 1;
    memcpy(buff+index, &_min,  1);
    index += 1;
    memcpy(buff+index, &_sec,  1);
    index += 1;
    return buff;
}
- (uint8_t)weekdayStringFromDate:(NSDate *)inputDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/BeiJing"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return theComponents.weekday;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _flag = 0;
        _wday = 0;
        _year = 0;
        _mouth = 0;
        _mday = 0;
        _hour = 0;
        _min = 0;
        _sec = 0;
    }
    return self;
}


-(void)setDateFromBData:(NSData *)bData{
    
    if (bData.length != 7) return;
    NSRange range ;
    range.length = 1;
    range.location = 0;
    [bData getBytes:&_sec range:range];
    range.location = 1;
    [bData getBytes:&_min range:range];
    range.location = 2;
    [bData getBytes:&_hour range:range];
    range.location = 3;
    [bData getBytes:&_mday range:range];
    range.location = 4;
    [bData getBytes:&_mouth range:range];
    range.location = 5;
    [bData getBytes:&_year range:range];
    range.location = 6;
    [bData getBytes:&_wday range:range];
    if (_year != 0) {
        _dimension = Linkage_Time_Dimension_All;
    }else if(_year == 0 && _mouth != 0){
        _dimension = Linkage_Time_Dimension_Year;
    }else if(_year == 0 && _mouth == 0 && _mday != 0){
        _dimension = Linkage_Time_Dimension_Mouth;
    }else{
        _dimension = Linkage_Time_Dimension_Day;
    }
}

-(NSString*)toStr
{
    int mouth = _mouth == 0 ? 1 : _mouth;
    int mday = _mday == 0 ? 1 : _mday;
    NSString* gmtStr = [NSString stringWithFormat:@"%llu/%.2d/%.2d %.2d:%.2d"
                        ,_year+2000,mouth,mday,_hour,_min];
    NSDateFormatter* format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy/MM/dd HH:mm";
    format.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSDate* dateGMT  = [format dateFromString:gmtStr];
    format.timeZone = [NSTimeZone localTimeZone];
    NSString* str = nil;
    if (_dimension == Linkage_Time_Dimension_Day)
    {
        format.dateFormat = @"每天hh:mm";
        str = [format stringFromDate:dateGMT];
    }
    else if (_dimension == Linkage_Time_Dimension_Mouth)
    {
        format.dateFormat = @"每月dd hh:mm";
        str = [format stringFromDate:dateGMT];
    }
    else if (_dimension == Linkage_Time_Dimension_Year)
    {
        format.dateFormat = @"每年MM/dd hh:mm";
        str = [format stringFromDate:dateGMT];
    }
    else
    {
        format.dateFormat = @"yy/MM/dd hh:mm";
        str = [format stringFromDate:dateGMT];
    }
    return str;
}
@end

